﻿
using Sys;

using System.Silverlight;
using System.Silverlight.Presentation;
using System.Silverlight.Presentation.Animation;
using System;
using seh = Sys.EventHandler;
using sea = Sys.EventArgs;

namespace SBT
{
    public class VerticalScrollBar : ScrollBarBase
    {
        protected override void ApplyMinusTick()
        {
            double newVal = ScrollContainer.ScrollY - 5;
            ScrollContainer.ScrollY = newVal > 0 ? newVal : 0;
            
        }
        protected override void ApplyPlusTick()
        {
            double newVal = ScrollContainer.ScrollY + 5;
            ScrollContainer.ScrollY = newVal > ScrollContainer.MaxScrollY ? ScrollContainer.MaxScrollY : newVal;
            
        }
        protected override string GetMinusSideMarginProperty()
        {
            return Grid.MarginTop;
        }
        protected override string GetPlusSideMarginProperty()
        {
            return Grid.MarginBottom;
        }
        protected override double GetBarContainerReferenceSize()
        {
            return BarContainer.Height;
        }
        protected override double GetCurrentScroll()
        {
            return ScrollContainer.ScrollY;
        }
        protected override double GetMaxScroll()
        {
            return ScrollContainer.MaxScrollY;
        }
        protected override double GetContentSize()
        {
            return ScrollContainer.Content.Height;
        }
        protected override void OnBarContainerSet()
        {
            base.OnBarContainerSet();
            MouseEventHandler mouseMoveHandler = OnMouseMove;
            BarContainer.Element.AddEventListener(InputEvent.MouseMove, mouseMoveHandler);
        }
        private void OnMouseMove(object sender, MouseEventArgs args)
        {
            if (Bar != null)
            {
                double y = args.GetPosition(BarContainer.Element).Y;
                double lastY = m_lastMouseY;
                if (lastY != y)
                {
                    if (Bar.IsMouseDown)
                    {
                        //GetContentSize() / GetBarContainerReferenceSize()
                        double barRatio = m_barRatio;
                        double newScroll = barRatio * (y - lastY) + ScrollContainer.ScrollY;
                        if (newScroll < 0)
                            newScroll = 0;
                        else if (newScroll > ScrollContainer.MaxScrollY)
                            newScroll = ScrollContainer.MaxScrollY;
                        ScrollContainer.ScrollY = newScroll;
                    }


                    m_lastMouseY = y;
                }
            }
        }
        
        private void ComputeBarRatio()
        {
            m_barRatio = ScrollContainer.Content.Height / BarContainer.Height;
            m_ratioComputed = true;
        }
        protected override double GetBarRatio()
        {
            if (!m_ratioComputed)
                ComputeBarRatio();
            return m_barRatio;
        }
        private bool m_ratioComputed;
        private double m_barRatio = 1;
        private double m_lastMouseY = 0;
    }
}